package com.fancy.mobileschool.ui;

import android.os.AsyncTask;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.fancy.mobileschool.MyConfiguration;
import com.fancy.mobileschool.R;
import com.fancy.mobileschool.entity.Person;
import com.fancy.mobileschool.service.UserService;


/**
 * A login screen that offers login via email/password.
 */
public class LoginActivity extends AppCompatActivity  implements Spinner.OnItemSelectedListener, View.OnClickListener{

    private Button signInButton;

    private Button signUpButton;

    private EditText idInput;

    private EditText pswdInput;

    private Spinner spinner;

    private String selectItem;

    private UserService userService;

    private static String[] selectItems = new String[]{"student", "password"};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        userService = new UserService();

        setContentView(R.layout.activity_login);
        signInButton = (Button) findViewById(R.id.sign_in_button);
        signUpButton = (Button) findViewById(R.id.sign_up_button);
        idInput = (EditText) findViewById(R.id.id);
        pswdInput = (EditText) findViewById(R.id.password);
        spinner = (Spinner) findViewById(R.id.login_select_spinner);

        spinner.setOnItemSelectedListener(this);
        signInButton.setOnClickListener(this);
        signUpButton.setOnClickListener(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return true;
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        selectItem = selectItems[position];
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }


    @Override
    public void onClick(View v) {
        String id = idInput.getText().toString();
        String pswd = pswdInput.getText().toString();
        if (id == null || pswd == null || id.equals("") || pswd.equals("") || selectItem == null) {
            Toast.makeText(getApplicationContext(), "请填写完整", Toast.LENGTH_SHORT).show();
            return;
        }


        if (v.getId() == R.id.sign_in_button) {
            String[] params = new String[]{id, pswd};
            new SignInTask().execute(params);
        } else if (v.getId() == R.id.sign_up_button) {
            Person person = new Person(id, "fancy", selectItem, pswd);
            new SignUpTask().execute(person);
        }
    }

    private class SignInTask extends AsyncTask<String, Void, Person> {

        @Override
        protected Person doInBackground(String... params) {
            return userService.signIn(params[0], params[1]);
        }

        @Override
        protected void onPostExecute(Person person) {
            if (person != null) {
                MyConfiguration.myName = person.name;
                MyConfiguration.type = person.type;
                MyConfiguration.myID = person.id;
                finish();
            }else
                Toast.makeText(getApplicationContext(), "ID或者密码不正确", Toast.LENGTH_SHORT).show();

        }
    }

    private class SignUpTask extends AsyncTask<Person, Void, Person> {

        @Override
        protected Person doInBackground(Person... params) {
            return userService.signUp(params[0]);
        }

        @Override
        protected void onPostExecute(Person person) {
            if (person != null) {
                MyConfiguration.myName = person.name;
                MyConfiguration.type = person.type;
                MyConfiguration.myID = person.id;
                finish();
            }else
                Toast.makeText(getApplicationContext(), "ID已被占用", Toast.LENGTH_SHORT).show();

        }
    }
}

